﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Authentication.OpenID;
using Marbles.Core.Content.CDN;
using Marbles.Core.Content.Pages;

namespace Marbles.Web
{
    public partial class Default : Marbles.Core.Pages.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckForAuthFlags();

            LiteralControl cHeaderContent = new LiteralControl();
            if (CurrentPage == null) { return; }
            CurrentPage.UpdateLastAccess();

            if (!string.IsNullOrEmpty(CurrentWeb.Language))
            {
                this.AddMetaTag("lang", "content-language", CurrentWeb.Language);
                this.AddMetaTag("xml:lang", "content-language", CurrentWeb.Language);
            }

            if (CurrentWeb.ExpiryHours == 0)
            {
                this.AddMetaTag("cache", "cache-control", "no-cache");
                this.AddMetaTag("expires", "expires", "0");
            }
            else
            {
                this.AddMetaTag("cache", "cache-control", "max-age=" + CurrentWeb.ExpiryHours.ToString() + ", no-cache, public");
                this.AddMetaTag("expires", "expires", DateTime.Now.AddHours(CurrentWeb.ExpiryHours).ToString("r"));
            }

            this.AddMetaTag("generator", "", "MarblesDOTnet", "generator");

            if (string.IsNullOrEmpty(CurrentPage.Title))
            {
                this.AddMetaTag("tweetTitle", "tweetmeme-title", CurrentWeb.Title);
                this.AddMetaTag("title", "title", CurrentWeb.Title);
                if (!string.IsNullOrEmpty(this.MasterPageFile)) {this.Title = CurrentWeb.Title;}
            }
            else
            {
                this.AddMetaTag("tweetTitle", "tweetmeme-title", CurrentWeb.Title + " - " + CurrentPage.Title);
                this.AddMetaTag("title", "title", CurrentWeb.Title + " - " + CurrentPage.Title);
                if (!string.IsNullOrEmpty(this.MasterPageFile)) { this.Title = CurrentWeb.Title + " - " + CurrentPage.Title; }
            }

            if (!string.IsNullOrEmpty(CurrentWeb.Robots)) { this.AddMetaTag("robots", "", CurrentWeb.Robots, "robots"); }
            if (!string.IsNullOrEmpty(CurrentPage.Description)) { this.AddMetaTag("description", "", CurrentPage.Description, "description"); }
            if (!string.IsNullOrEmpty(CurrentPage.Keywords)) { this.AddMetaTag("keywords", "", CurrentPage.Keywords, "keywords"); }
            if (!string.IsNullOrEmpty(CurrentPage.ManagingEditor)) { this.AddMetaTag("author", "", CurrentPage.ManagingEditor, "author"); }

            if (CurrentPage.AllowFeedDiscovery)
            {
                this.AddRSSFeedDiscoveryTag(this.PageName, CurrentPage.Title, "default.rss?id="+this.PageName);
            }

            //cContentToolbar.Visible = (this.IsEditor || this.IsAdministrator);
            //cContentToolbar.Visible = true;
            //RegisterClientScriptHeaderInclude("Editor", Resources.ResourceHandler.GetContentEditorJavaScript(this));
        }

        private void CheckForAuthFlags()
        {
            if (this.AuthenticationProvider.Provider == "OpenId")
            {
                OpenIdProvider prov = this.AuthenticationProvider as OpenIdProvider;
                if (prov.IsOpenIdRequest)
                {
                    OpenIdToken data = prov.Authenticate() as OpenIdToken;

                    if (data.Success)
                    {
                        Response.Redirect(Request.Path, true);
                    }
                }

            }
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            //Do nothing!
        }

    }
}